<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="internshipQuery.name" placeholder="用户名" />
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="internshipQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="internshipQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table :data="list" border fit highlight-current-row>
      <el-table-column label="ID号  " width="70" align="center">
        <template slot-scope="scope">
          <!-- {{ (page - 1) * limit + scope.$index + 1 }} -->
          {{ scope.row.sid }}
        </template>
      </el-table-column>

      <el-table-column label="学号  " width="70" align="center">
        <template slot-scope="scope">
          <!-- {{ (page - 1) * limit + scope.$index + 1 }} -->
          {{ scope.row.stuId }}
        </template>
      </el-table-column>

      <el-table-column prop="sid" label="学生名" width="200">
        <template slot-scope="scope">
          {{ scope.row.stuName }}
        </template>
      </el-table-column>

      <el-table-column prop="intern" label="企业名" width="200" v-if="role == '管理员'">
        <template slot-scope="scope">
          {{ scope.row.internName }}
        </template>
      </el-table-column>

      <el-table-column prop="internshipRecordStatus" label="实习完成状态" width="200">
        <template slot-scope="scope">
          {{ scope.row.internshipRecordStatus ? "已完成" : "未完成" }}
        </template>
      </el-table-column>

      <el-table-column prop="internshipSummaryStatus" label="实习总结状态" width="200">
        <template slot-scope="scope">
          {{ scope.row.internshipSummaryStatus ? "已完成" : "未完成" }}
        </template>
      </el-table-column>

      <el-table-column prop="teacherGrade" label="指导教师分数" />
      <el-table-column prop="internshipGrade" label="实习单位分数" />

      <el-table-column prop="internshipEndTime" label="实习完成时间">
        <template slot-scope="scope">
          {{ scope.row.internshipEndTime }}
        </template>
      </el-table-column>

  <el-table-column prop="status" label="完成状态">
        <template slot-scope="scope">
          {{ scope.row.status==2?'成功':'失败' }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <router-link :to="'/recode/edit/' + scope.row.sid">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改记录</el-button>
          </router-link>

          <el-button
            type="success"
            size="mini"
            plain
            @click="deleteInternshipById(scope.row.id)"
            >删除记录</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改评论" :visible.sync="updateEvaluateVisible">
      <el-form :model="evaluateObj">
        <el-form-item label="评论人" :label-width="formLabelWidth">
          <el-input v-model="evaluateObj.uname" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="评论对象" :label-width="formLabelWidth">
          <el-input
            disabled
            v-model="evaluateObj.targetName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="评论" :label-width="formLabelWidth">
          <el-input
            v-model="evaluateObj.evaluate"
            type="textarea"
            maxlength="200"
            show-word-limit
            rows="6"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="updateEvaluateVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateEvaluateById(evaluateObj)"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />

    <div id="main" style="width: 50%; height: 600px"></div>
  </div>
</template>

<script>
import evaluateApi from "@/api/evaluate";
import recodeApi from "@/api/recode";
import { mapGetters } from "vuex";

export default {
  computed: {
    ...mapGetters(["name", "roles"]),
  },
  data() {
    return {
      option: {},
      role: "",
      list: null,
      page: 1, //当前页
      limit: 10, //每页记录数
      total: 0, //总记录数
      internshipQuery: {}, //条件封装对象
      //学生列表
      evaluateObj: {
        uname: "",
        targetName: "",
        evaluate: "",
      },
      updateEvaluateVisible: false,
      formLabelWidth: "120px",
    };
  },
  created() {
    this.getList();
    this.getStuStatus();
  },
  mounted() {
    this.drawChart();
  },

  methods: {
    async drawChart() {
      var data = {};
      await recodeApi.getStuStatus1().then((resp) => {
        console.log(resp.data);
        data = resp.data;
      });

      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById("main"));
      // 指定图表的配置项和数据
      let option = (this.option = {
        title: {
          text: "实习完成情况",
        },
        tooltip: {},
        legend: {
          data: ["数量"],
        },
        xAxis: {
          data: data.text,
        },
        yAxis: {},
        series: [
          {
            name: "数量",
            type: "bar",
            data: data.nums,
          },
        ],
      });
      console.log(option);
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },

    //获取学生实习状态
    getStuStatus() {
      recodeApi.getStuStatus1().then((resp) => {
        console.log(resp.data);
        return resp.data;
      });
    },

    getList(page = 1) {
      this.page = page;
      this.role = this.roles[0];
      if (this.role == "学校管理员" || this.role == "企业管理员") {
        this.internshipQuery.associationName = this.name;
      }
      recodeApi
        .getInternshipList(this.page, this.limit, this.internshipQuery)
        .then((response) => {
          this.list = response.data.userInternships;
          this.total = response.data.total;
        });
    },

    //打开修改记录
    updateEvaluate(id) {
      //将id赋值都vue对象参数内
      this.evaluateId = id;
      this.updateEvaluateVisible = true;
      //获取当前选中的评价行记录
      evaluateApi.getEvaluateInfo(id).then((response) => {
        this.evaluateObj = response.data.evaluate;
      });
    },

    //修改评论
    updateEvaluateById(evaluate) {
      evaluateApi.updateEvaluateById(evaluate).then((resp) => {
        this.$message({
          type: "success",
          message: "更新成功!",
        });
        this.updateEvaluateVisible = false;
        this.getList();
      });
    },

    //删除评价
    deleteInternshipById(id) {
      this.$confirm("此操作将删除实习记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        recodeApi.deleteInternship(id).then((response) => {
          //提示信息
          this.$message({
            type: "success",
            message: "移除成功!",
          });
          //再次渲染
          this.getList();
        });
      });
    },
  },
};
</script>

<style></style>
